﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GGJ_DKG.Objects;
using GGJ_DKG.Menu;
using GGJ_DKG.Managers;


namespace GGJ_DKG.Objects
{
    abstract class Sprite:Object2D
    {
        public int direita = 1;
        protected float camada;
        protected Vector2 pivo;
        protected Rectangle origem;
        protected Rectangle destino;
        protected animacao animacao_atual;
        protected SpriteEffects spriteEffects;

        protected Rectangle colisao;
        public Rectangle Colisao
        {
            get
            {
                return new Rectangle((int)(Position.X +(MiddlePoint.X / 2)), (int)(Position.Y + (MiddlePoint.Y / 2)), 
                    animacao_atual.quadro_X, animacao_atual.quadro_Y);
            }
            set
            {
                colisao = value;
            }
        }

        public struct animacao
        {
            public int quadro_X;//get set origem destino
            public int quadro_Y;
            public int qtd_quadros;
            public int quadros_seg;
            public int Y_inicial;
            public bool ativa;
            public int quadro_atual;
        }

        public Sprite(Texture2D textura)
            : base(textura)
        {
            this.pivo = new Vector2(destino.Width / 2, destino.Height / 2);//influencia tudo...
            this.colisao = new Rectangle((int)origem.X, (int)origem.Y, origem.Width, origem.Height);
            
        }

        public override abstract void Update(GameTime gameTime);
        /// <summary>
        /// Animated Update
        /// </summary>
        /// <param name="gameTime"></param>
        public void AnimatedUpdate(GameTime gameTime)
        {
            int frame = (int)(gameTime.TotalGameTime.TotalSeconds *
                animacao_atual.quadros_seg) % animacao_atual.qtd_quadros;
            origem = new Rectangle(
                            (int)Position.X,
                            (int)Position.Y,
                            animacao_atual.quadro_X,
                            animacao_atual.quadro_Y);
            destino = new Rectangle(
                            frame * animacao_atual.quadro_X,
                            animacao_atual.Y_inicial,
                            animacao_atual.quadro_X,
                            animacao_atual.quadro_Y);
            MiddlePoint = new Vector2(destino.Width / 2, destino.Height / 2);
            switch (direita)
            {
                case 0:
                    if (Rotation == MathHelper.ToRadians(0))
                    {
                        spriteEffects = SpriteEffects.None;
                    }
                    else { spriteEffects = SpriteEffects.None; }
                    break;

                case 1:
                    if (Rotation == MathHelper.ToRadians(0))
                    {
                        spriteEffects = SpriteEffects.FlipHorizontally;
                    }
                    else { spriteEffects = SpriteEffects.None; }
                    break;
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(Texture, origem,destino, ObjectColor, Rotation, MiddlePoint, spriteEffects, camada);
        }

        new public bool hitTestObject(Rectangle collider)
        {
            this.calculateRectangle();

            if (this.colisao.Intersects(collider))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
